import 'dart:io';

import 'package:flutter/material.dart';
import 'package:flutter_mode/utils.dart';
import 'package:flutter_mode/zyj/ui/image_picker.dart';
import 'package:flutter_mode/zyj/ui/webview.dart';
import 'package:photo_view/photo_view.dart';

class PhotoDemoPage extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    // TODO: implement createState
    return _PhotoDemoPageState();
  }
}

class _PhotoDemoPageState extends State<PhotoDemoPage>
    with SingleTickerProviderStateMixin {
  var _tabctr;
  File? _image;

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    _tabctr = TabController(length: 2, vsync: this);
  }

  @override
  Widget build(BuildContext context) {
    var items = {
      '使用image_picker（未选择图片的原因没有被返回，比如没权限还是用户取消了操作）': () =>
          pushMaterial(context, ImagePickerPage()),
      '使用photo（可多选图片，使用的glide加载图片地址。其依赖photo_manager，也可使用photo_manager导入图片自定义界面）':
          _photoUse,
      'camera 最sdk 21，暂时不测试': () {},
      '': () {},
      'image_crop 裁剪': () {
        if (_image == null) {
          _photoUse();
          showToast('请先选则图片');
        } else {
          showDialog(
              context: context,
              builder: (context) => Center(
                    child: Container(
                      color: Colors.black,
                      padding: EdgeInsets.all(10),
                      // child: Crop.file(_image!),
                      // child: Crop(image: _image!.readAsBytesSync(), onCropped: (data){}),
                      child: Text('xxxxxx'),
                    ),
                  ),
              barrierDismissible: true);
        }
      },
      ' ': () {},
      'image_cropper库需要在AndroidManifest配置Activity, 在集成模式下不方便测试，这里就不做测试。'
              '如果image_crop有缺陷再看image_cropper库怎么样。image_crop 还提供了无界面，代码裁剪图片的方法。image_crop的文档：https://pub.flutter-io.cn/packages/image_crop':
          () {
        pushMaterial(
            context,
            WebViewPage(
              title: 'doc',
              url: 'https://pub.flutter-io.cn/packages/image_crop',
            ));
      },
    };

    // TODO: implement build
    return Scaffold(
      appBar: AppBar(
        title: Text('PhotoDemo'),
        bottom: TabBar(
          tabs: [
            Tab(
              text: '选择操作',
            ),
            Tab(
              text: '展示图片',
            ),
          ],
          controller: _tabctr,
        ),
      ),
      body: TabBarView(
        children: [
          ListView.separated(
            itemBuilder: (context, position) {
              var item = items.entries.elementAt(position);
              return ListTile(
                title: Text(item.key),
                onTap: item.value,
                trailing: Icon(Icons.keyboard_arrow_right),
              );
            },
            itemCount: items.length,
            separatorBuilder: (context, position) => Divider(
                  height: 1,
                ),
          ),
          new Center(
            child: _image == null
                ? new Text('No image selected.')
                : new PhotoView(imageProvider: FileImage(_image!)),
          )
        ],
        controller: _tabctr,
      ),
    );
  }

  _photoUse() async {
    /*List<AssetEntity> imgList = await PhotoPicker.pickAsset(
        context: context,
        // BuildContext requied

        /// The following are optional parameters.
        themeColor: Colors.green,
        // the title color and bottom color
        padding: 1.0,
        // item padding
        dividerColor: Colors.grey,
        // divider color
        disableColor: Colors.grey.shade300,
        // the check box disable color
        itemRadio: 0.88,
        // the content item radio
        maxSelected: 8,
        // max picker image count
        provider: I18nProvider.chinese,
        // i18n provider ,default is chinese. , you can custom I18nProvider or use ENProvider()
        rowCount: 5,
        // item row count
        textColor: Colors.white,
        // text color
        thumbSize: 150,
        // preview thumb size , default is 64
        sortDelegate: SortDelegate.common,
        // default is common ,or you make custom delegate to sort your gallery
        checkBoxBuilderDelegate: DefaultCheckBoxBuilderDelegate(
          activeColor: Colors.white,
          unselectedColor: Colors.white,
        ),
        // default is DefaultCheckBoxBuilderDelegate ,or you make custom delegate to create checkbox

        loadingDelegate: this,
        // if you want to build custom loading widget,extends LoadingDelegate [see example/lib/main.dart]
        pickType: PickType.onlyImage //仅选择图片
        );

    if (imgList == null) {
    } else {
      for (var e in imgList) {
        _image = await e.file;
        print(_image.path);
      }
    }
    setState(() {});*/
  }

  /*@override
  Widget buildBigImageLoading(
      BuildContext context, AssetEntity entity, Color themeColor) {
    return Center(
      child: Container(
        width: 50.0,
        height: 50.0,
        child: CircularProgressIndicator(),
      ),
    );
  }

  @override
  Widget buildPreviewLoading(
      BuildContext context, AssetEntity entity, Color themeColor) {
    return Center(
      child: Container(
        width: 50.0,
        height: 50.0,
        child: CircularProgressIndicator(),
      ),
    );
  }*/
}
